package com.song.controller.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.song.pojo.Items;
import com.song.service.ItemsService;
import com.song.utils.DataGridViewResult;
import com.song.utils.SystemConstant;
import com.song.vo.ItemsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 宋超
 * HP - the login name of the current user.
 * 2020/12/21 - the current system date.
 * 21:43 - the current system time.
 */
@RestController
@RequestMapping("/admin/items")
public class ItemsController {


    @Autowired
    private ItemsService itemsService;

    /**
     * 查询商品列表
     * @param itemsVo
     * @return
     */
    @RequestMapping("/list")
    private DataGridViewResult list(ItemsVo itemsVo){
        //设置分页信息
        PageHelper.startPage(itemsVo.getPage(),itemsVo.getLimit());
        //查询类型列表
        List<Items> itemsList = itemsService.findItemsList(itemsVo);
        //创建分页对象
        PageInfo<Items> pageInfo = new PageInfo<Items>(itemsList);
        //返回数据
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 添加商品
     * @param items
     * @return
     */
    @RequestMapping("/addItems")
    public String addRoomType(Items items){
        Map<String,Object> map = new HashMap<String,Object>();
        if(itemsService.addItems(items)>0){
            map.put(SystemConstant.SUCCESS,true);//成功
            map.put(SystemConstant.MESSAGE,"添加成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);//失败
            map.put(SystemConstant.MESSAGE,"添加失败");
        }
        return JSON.toJSONString(map);
    }


    /**
     * 修改商品
     * @param items
     * @return
     */
    @RequestMapping("/updateItems")
    public String updateRoomType(Items items){
        Map<String,Object> map = new HashMap<String,Object>();
        if(itemsService.updateItems(items)>0){
            map.put(SystemConstant.SUCCESS,true);//成功
            map.put(SystemConstant.MESSAGE,"修改成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);//失败
            map.put(SystemConstant.MESSAGE,"修改失败");
        }
        return JSON.toJSONString(map);
    }


    /**
     * 删除员工
     * @param id
     * @return
     */
    @RequestMapping("/deleteById")
    public String deleteById(int id){
        Map<String,Object> map = new HashMap<String,Object>();
        //调用删除商品的方法
        if(itemsService.deleteById(id)>0){
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, "删除成功");
        }else{
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, "删除失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 查询所有商品类型
     * @return
     */
    @RequestMapping("/findAll")
    public String findAll(){
        return JSON.toJSONString(itemsService.findItemsList(null));
    }


}
